using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using JSG.BubbleShoot;

namespace JSG.BubbleShoot
{
    public class Candy : MonoBehaviour
    {
        public SpriteRenderer m_Image;
        [HideInInspector]
        public int m_ColorNum = 0;

        public AudioSource m_Sound;

        public Sprite[] m_ColorSprites;

        [HideInInspector]
        public bool m_Scale;

        public bool m_GotHit = false;
        public bool m_IsShaking = false;

        public List<Candy> m_NearCandies;
        public GameObject[] m_Particles;

        private void Awake()
        {
            m_NearCandies = new List<Candy>();
        }
        // Start is called before the first frame update
        void Start()
        {
            m_Scale = false;


            m_GotHit = false;
            m_IsShaking = false;
        }

        // Update is called once per frame
        void Update()
        {
            if (m_Scale)
            {
                transform.localScale += 1 * Time.deltaTime * Vector3.one;
            }

        }

        public void SetColor(int num)
        {
            m_ColorNum = num;
            m_Image.sprite = m_ColorSprites[num];
        }

        public void Scale()
        {
            m_Scale = true;
            Invoke("Destroy", .3f);
        }

        IEnumerator Co_Hit(float delay = 0)
        {

            yield return new WaitForSeconds(delay);
            //m_Sound.Play();
            float lerp = 0;
            while (lerp < 1)
            {
                transform.localScale = Vector3.Lerp(Vector3.one, 1.3f * Vector3.one, lerp);
                lerp += 15 * Time.deltaTime;
                yield return null;
            }


            Destroy();
        }
        public void Hit(float delay = 0, bool first = true)
        {
            if (!m_GotHit)
            {
                m_GotHit = true;
                GameControl.m_Current.m_WaitTime = 0;
                if (first)
                {
                    //
                }
                //float delay = .1f;
                //foreach (Candy candy in m_NearCandies)
                //{
                //    if (!candy.m_GotHit && candy.gameObject.activeSelf)
                //    {
                //        candy.Hit(0);
                //        delay += .2f;
                //    }
                //}
                StartCoroutine(Co_Hit(delay));
            }

        }

        public void Shake(Vector3 dir, float dis = 5, float delay = 0)
        {
            if (!m_IsShaking)
            {
                StartCoroutine(Co_Shake(dir, dis, delay));
            }
        }
        IEnumerator Co_Shake(Vector3 dir, float dis = 5, float delay = 0)
        {
            m_IsShaking = true;
            yield return new WaitForSeconds(delay);
            Vector3 startPos = transform.position;

            float lerp = 0;
            while (lerp < 1)
            {
                transform.transform.position = startPos + dir * (dis * Mathf.Sin(lerp * Mathf.PI));
                lerp += 6 * Time.deltaTime;
                yield return null;
            }

            transform.transform.position = startPos;
            m_IsShaking = false;
        }

        public void Destroy()
        {
            float delay = .02f;
            foreach (Candy candy in m_NearCandies)
            {
                if (!candy.m_GotHit && candy.gameObject.activeSelf)
                {
                    candy.Hit(delay);
                    delay += .02f;
                }
            }

            DestroyAlone();
        }

        public void DestroyAlone()
        {
            GameObject obj = Instantiate(m_Particles[m_ColorNum]);
            obj.transform.position = transform.position + new Vector3(0, 0, -2);
            Destroy(obj, 2);
            GameControl.m_Current.m_ColorCount[m_ColorNum]--;
            GameControl.m_Current.m_CandyCounter++;
            gameObject.SetActive(false);
        }
    }
}